require('dotenv').config()
const express = require('express')
const app = express()
const cors = require('cors')
const { expressjwt: jwt } = require('express-jwt')

// 中间件
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(express.static('public'))
app.use(cors())
app.use(
  jwt({
    secret: process.env.JWT_SECRET,
    algorithms: ['HS256'],
  }).unless({
    path: ['/api/auth/login', '/api/auth/register'],
  })
)

// 路由
app.use('/api/auth', require('./routes/auth'))

// 错误处理
app.use((err, req, res, next) => {
  res.status(500).json({ message: err.message })
})

app.listen(process.env.PORT, () => {
  console.log(`Example app listening at http://localhost:${process.env.PORT}`)
})
